<template>
<div>
    <el-dialog :visible.sync="dataForm.dialog" size="tiny">
        <p class="dialog-title">{{dataForm.sClassName}}</p>
        <el-form :model="form" ref="form" label-width="150px" :rules="rules">
            <el-col :span="15" :offset="3" v-for="(item,index) in form.sNameList" :key="index">
                <el-form-item :label="item.sRatingName+'（'+item.score+'）'" :prop="'sNameList.' + index + '.sRatingResult'" :rules="validate(index)">
                    <el-input v-model="item.sRatingResult" placeholder="请输入评分" :maxlength="3"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="10" :offset="10" class="rating-annotation">
                提示：评分不可超过设置的最高分
            </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer footer">
            <p class="btn">
            <el-button @click="resetFormdelDialogAddTea('form')">取 消</el-button>
            <el-button type="primary" @click="adddataForm('form')" :loading="loadingBtn">确 认</el-button>
            </p>
        </div>
    </el-dialog>
</div>
</template>
<script>
import { getRatingInfo, modifyRatingInfo} from 'api/wisdom';
import { mapGetters } from 'vuex';
export default {
    props: {
        dataForm: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            loadingBtn: false,
            form: {
                sNameList: []
            },
            rules: {
            }
        }
    },
    computed: {
        ...mapGetters([
            'schoolId'
        ])
    },
    methods: {
        adddataForm(dataForm) {
            this.$refs[dataForm].validate((valid) => {
                if (valid) {
                    let apiWisdomRatingMap = [];
                    this.form.sNameList.forEach((item, i) => {
                        let obj = {
                            sRatingId: '',
                            sRatingResult: ''
                        }
                        obj.sRatingId = item.sRatingId;
                        obj.sRatingResult = item.sRatingResult;

                        apiWisdomRatingMap.push(obj);
                    })
                    console.log(apiWisdomRatingMap, 'apiWisdomRatingMap');
                    this.loadingBtn = true;
                    modifyRatingInfo(this.dataForm.fClassId, this.dataForm.sClassName, apiWisdomRatingMap).then(res => {
                        this.loadingBtn = false;
                        this.$message({
                            type: 'success',
                            message: '修改评分成功'
                        })
                        this.delDialogAddTea();
                        this.$emit('success')
                    }).catch(() => {
                        this.loadingBtn = false;
                    })
                } else {
                    return false;
                }
            });
        },
        validate(index) {
            return [
                {
                    required: true,
                    validator: (rule, value, callback) => {
                        let reg = /^([1-9]\d?|100)$/;
                        if (value === '') {
                            callback(new Error('评分不能为空'));
                        } else if (!reg.test(Number(value))) {
                            callback(new Error('请输入1到100之间的正整数'));
                        } else if (Number(value) > this.form.sNameList[index].score) {
                            callback(new Error('评分不可超过设置的最高分'));
                        } else {
                            callback();
                        }
                    },
                    trigger: 'change, blur'
                }
            ];
        },
        getRating() {
            getRatingInfo(this.schoolId, this.dataForm.fClassId).then(res => {
                this.form.sNameList = res.data.content;
                this.form.sNameList.forEach((item, index) => {
                    this.form.sNameList[index].sRatingResult = item.sRatingResult == '0' ? '' : item.sRatingResult;
                });
            });
        },
        handleClose() {
            this.$refs.form.resetFields();
            this.delDialogAddTea();
        },
    // 取消按钮 
        resetFormdelDialogAddTea(dataForm) {
            this.$refs.form.resetFields();
            this.delDialogAddTea();
        },
    // 还原清空弹窗信息
        delDialogAddTea() {
            this.$refs.form.resetFields();
            this.dataForm.dialog = false;
            this.form.sNameList = [];
        }
    }
}
</script>
<style lang="scss" scoped>
    .el-form{
        padding-left:5px;
    }
    .dialog-title{
        width:60%;
        margin:0px auto;
    }
    .footer{
        padding: 25px;
    }
    .rating-annotation{
        color: #ff5e2c;
        margin-top:10px;
        font-size: 12px;
    }
</style>


